package cn.net.chenbao.medical;

import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.OptionsPickerView;


import java.util.ArrayList;
import java.util.List;

import cn.net.chenbao.medical.adapter.PatientAdapter;
import cn.net.chenbao.medical.adapter.radapter.CommonViewHolder;
import cn.net.chenbao.medical.adapter.radapter.RvCommonAdapter;
import cn.net.chenbao.medical.base.BaseActivity;
import cn.net.chenbao.medical.bean.Ks;
import cn.net.chenbao.medical.bean.Patient;
import cn.net.chenbao.medical.bean.Pyj;
import cn.net.chenbao.medical.bean.Report;
import cn.net.chenbao.medical.mvp.AbnormalP;
import cn.net.chenbao.medical.mvp.presenter.IAbnormalP;
import cn.net.chenbao.medical.mvp.view.IAbnormalV;
import cn.net.chenbao.medical.util.Consts;
import cn.net.chenbao.medical.util.TimeUtil;
import cn.net.chenbao.medical.util.WWToast;
import cn.net.chenbao.medical.util.ZLog;

public class AbnormalActivity extends BaseActivity implements IAbnormalV, View.OnClickListener {
    private int mMode;
    private int mPyFlag;
    private long mPrintId;
    private TextView mTvLeft, mTvTitle, mTvRight;
    private String mStartDate;

    private RecyclerView mRvPatient;
    private RvCommonAdapter mPatientAdapter;
    private List<Patient> mPatients;

    private IAbnormalP mAbnormalP;
    private int position;
    LinearLayoutManager mLayoutManager;

    private String mPyjNo;
    private List<Pyj> mPyjs;

    private String mKsbmNo;
    private List<Ks> mKsList;

    private OptionsPickerView mPyjPv;
    private OptionsPickerView mKsPv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_abnormal);
        mAbnormalP = new AbnormalP(this);
        InitTitle();
        InitView();
    }

    private void InitTitle() {
        mMode = getIntent().getIntExtra(Consts.KEY_MODULE, -1);
        mStartDate = getIntent().getStringExtra("startDate");
        findViewById(R.id.iv_left).setOnClickListener(this);
        mTvTitle = (TextView) findViewById(R.id.tv_center);
        mTvRight = (TextView) findViewById(R.id.tv_date_select);
        switch (mMode) {
            case Consts.MODE_JC:
                //进仓报表
                mTvRight.setVisibility(View.VISIBLE);
                mTvRight.setText("查询(S2键)");
                mTvRight.setOnClickListener(this);
                mTvTitle.setText(getString(R.string.in_abnormal));
                mPrintId = getIntent().getLongExtra("printId", -1);
                break;
            case Consts.MODE_NURSE:
                //配液报表
                mTvRight.setVisibility(View.VISIBLE);
                mTvRight.setText("查询(S2键)");
                mTvRight.setOnClickListener(this);

                mTvTitle.setText(getString(R.string.py_abnormal));
                mPrintId = getIntent().getLongExtra("printId", -1);
                break;
            case Consts.MODE_KBCC:
                //空包出仓报表
                mTvRight.setVisibility(View.VISIBLE);
                mTvRight.setText("查询(S2键)");
                mTvRight.setOnClickListener(this);

                mTvTitle.setText(getString(R.string.out_abnormal));
                mPrintId = getIntent().getLongExtra("printId", -1);
                break;
            case Consts.MODE_APOTHECARY:
                //出仓报表
                mTvTitle.setText(getString(R.string.out_abnormal));
                mPrintId = getIntent().getLongExtra("printId", -1);
                break;
            case Consts.MODE_HSJJ:
                //交接报表
                mTvTitle.setText(getString(R.string.jiao_abnormal));
                mPyFlag = getIntent().getIntExtra("pyFlag", -1);
                break;
        }
    }

    private void InitView() {
        mRvPatient = (RecyclerView) findViewById(R.id.rv_abnormal);
        mLayoutManager = new LinearLayoutManager(this);
        mRvPatient.setLayoutManager(mLayoutManager);
        mPatients = new ArrayList<>();
        mPatientAdapter = new PatientAdapter(this, mPatients, R.layout.abnormal_item);
        mRvPatient.setAdapter(mPatientAdapter);

        mAbnormalP.doAbnormal(mMode, mStartDate, mPyFlag, mPrintId, mPyjNo, mKsbmNo);
    }

    @Override
    public void InitPyjListSuccess(List<Pyj> pyjs) {
        if (pyjs == null || pyjs.size() == 0) {
            WWToast.showShort("配液间列表为空");
            return;
        }
        this.mPyjs = pyjs;
        ShowPyjPv(mPyjs);
    }

    public void ShowPyjPv(final List<Pyj> datas) {
        if (mPyjPv == null) {
            mPyjPv = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int options2, int options3, View v) {
                    mPyjNo = datas.get(options1).ItemClsno;
                    mAbnormalP.doAbnormal(mMode, mStartDate, mPyFlag, mPrintId, mPyjNo, mKsbmNo);
                }
            })
                    .setDividerColor(ContextCompat.getColor(this, R.color.main_line_color))
                    .setTextColorCenter(ContextCompat.getColor(this, R.color.main_color)) //设置选中项文字颜色
                    .setTextColorOut(ContextCompat.getColor(this, R.color.black2))
                    .setCancelColor(ContextCompat.getColor(this, R.color.black))
                    .setSubmitColor(ContextCompat.getColor(this, R.color.main_color))
                    .setTitleText("选择配液间")
                    .setTitleSize(13)
                    .setTitleBgColor(ContextCompat.getColor(this, R.color.white))
                    .setBgColor(ContextCompat.getColor(this, R.color.white))
                    .setContentTextSize(16)
                    .setSubCalSize(16)
                    .build();
        }
        mPyjPv.setPicker(datas);
        mPyjPv.show();
    }


    @Override
    public void InitKsListSuccess(List<Ks> ksList) {
        if (ksList == null || ksList.size() == 0) {
            WWToast.showShort("科室列表为空");
            return;
        }
        this.mKsList = ksList;
        ShowKsPv(mKsList);
    }

    public void ShowKsPv(final List<Ks> datas) {
        if (mKsPv == null) {
            mKsPv = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int options2, int options3, View v) {
                    mKsbmNo = datas.get(options1).No;
                    mAbnormalP.doAbnormal(mMode, mStartDate, mPyFlag, mPrintId, mPyjNo, mKsbmNo);
                }
            })
                    .setDividerColor(ContextCompat.getColor(this, R.color.main_line_color))
                    .setTextColorCenter(ContextCompat.getColor(this, R.color.main_color)) //设置选中项文字颜色
                    .setTextColorOut(ContextCompat.getColor(this, R.color.black2))
                    .setCancelColor(ContextCompat.getColor(this, R.color.black))
                    .setSubmitColor(ContextCompat.getColor(this, R.color.main_color))
                    .setTitleText("选择科室")
                    .setTitleSize(13)
                    .setTitleBgColor(ContextCompat.getColor(this, R.color.white))
                    .setBgColor(ContextCompat.getColor(this, R.color.white))
                    .setContentTextSize(16)
                    .setSubCalSize(16)
                    .build();
        }
        mKsPv.setPicker(datas);
        mKsPv.show();
    }

    @Override
    public void InitAbnormalSuccess(List<Patient> patients) {
        this.mPatients = patients;
        mPatientAdapter.setDatas(mPatients);
        mPatientAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_left: {
                finish();
                break;
            }
            case R.id.tv_date_select: {
                if (mMode == Consts.MODE_JC || mMode == Consts.MODE_NURSE) {
                    if (mPyjs == null)
                        mAbnormalP.PyjList();
                    else
                        ShowPyjPv(mPyjs);
                } else if (mMode == Consts.MODE_KBCC) {
                    //空包出仓新增科室查询
                    if (mKsList == null)
                        mAbnormalP.KsList();
                    else
                        ShowKsPv(mKsList);
                }
                break;
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (event.getKeyCode()) {
            case 19:
                //上
                if (mPyjPv != null && mPyjPv.isShowing()) {
                    mPyjPv.SetOption1CurrentItemPre(mPyjs.size());
                    break;
                }
                if (mKsPv != null && mKsPv.isShowing()) {
                    mKsPv.SetOption1CurrentItemPre(mKsList.size());
                    break;
                }
                position = mLayoutManager.findFirstVisibleItemPosition();
                ZLog.e(position + "=================");
                if (mPatients == null)
                    break;
                if (position >= 0) {
                    --position;
                    MoveToPosition(mLayoutManager, mRvPatient, position);
                }
                break;
            case 20:
                //下
                if (mPyjPv != null && mPyjPv.isShowing()) {
                    mPyjPv.SetOption1CurrentItemNext(mPyjs.size());
                    break;
                }
                if (mKsPv != null && mKsPv.isShowing()) {
                    mKsPv.SetOption1CurrentItemNext(mKsList.size());
                    break;
                }
                position = mLayoutManager.findFirstVisibleItemPosition();
                ZLog.e(position + "=================");
                if (mPatients == null)
                    break;
                if (position < mPatients.size() - 1 && !(mLayoutManager.findLastVisibleItemPosition() > mPatients.size() - 1)) {
                    ++position;
                    MoveToPosition(mLayoutManager, mRvPatient, position);
                }
                break;
            case 6:
                //S2键
                if (mMode == Consts.MODE_JC || mMode == Consts.MODE_NURSE) {
                    if (mPyjs == null)
                        mAbnormalP.PyjList();
                    else
                        ShowPyjPv(mPyjs);
                } else if (mMode == Consts.MODE_KBCC) {
                    if (mKsList == null)
                        mAbnormalP.KsList();
                    else
                        ShowKsPv(mKsList);
                }
                break;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (KeyEvent.KEYCODE_ENTER == event.getKeyCode() && KeyEvent.ACTION_UP == event.getAction()) {
            //处理事件
            if (mPyjPv != null && mPyjPv.isShowing()) {
                mPyjPv.returnData();
                mPyjPv.dismiss();
                return true;
            }
            if (mKsPv != null && mKsPv.isShowing()) {
                mKsPv.returnData();
                mKsPv.dismiss();
                return true;
            }

        }
        return super.dispatchKeyEvent(event);
    }

    /**
     * RecyclerView 移动到当前位置，
     *
     * @param manager       设置RecyclerView对应的manager
     * @param mRecyclerView 当前的RecyclerView
     * @param n             要跳转的位置
     */
    public void MoveToPosition(LinearLayoutManager manager, RecyclerView mRecyclerView, int n) {

        int firstItem = manager.findFirstVisibleItemPosition();
        int lastItem = manager.findLastVisibleItemPosition();

        ZLog.e(firstItem + "========" + lastItem + "======" + position);
        if (n <= firstItem) {
            if (n != -1) {
                mRecyclerView.scrollToPosition(n);
            }
        } else if (n <= lastItem) {
            int top = mRecyclerView.getChildAt(n - firstItem).getTop();
            mRecyclerView.scrollBy(0, top);
        } else {
            mRecyclerView.scrollToPosition(n);
        }

    }
}
